package com.i2works.smartluolong.manage.service.admin.impl;


import com.i2works.smartluolong.manage.dal.admin.dao.IAdminRoleDAO;
import com.i2works.smartluolong.manage.dal.admin.dao.IAdminUserDAO;
import com.i2works.smartluolong.manage.entity.admin.AdminRoleDO;
import com.i2works.smartluolong.manage.entity.admin.AdminRoleQuery;
import com.i2works.smartluolong.manage.entity.admin.AdminRoleResDO;
import com.i2works.smartluolong.manage.entity.admin.AdminUserRoleDO;
import com.i2works.smartluolong.manage.service.admin.IAdminRoleService;
import com.i2works.smartluolong.utils.basecommon.BaseCommonServiceImpl;
import com.i2works.smartluolong.utils.exception.DAOException;
import com.i2works.smartluolong.utils.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

@Service("adminRoleService")
public class AdminRoleServiceImpl extends BaseCommonServiceImpl<AdminRoleQuery, AdminRoleDO> implements IAdminRoleService {

    @Autowired
    private IAdminRoleDAO adminRoleDAO;
    @Autowired
    private IAdminUserDAO adminUserDAO;


    @Override
    public Set<AdminRoleDO> queryUserRoleList(AdminRoleQuery query) throws ServiceException {
        try {
            return adminRoleDAO.queryUserRoleList(query);
        } catch (DAOException e) {
            throw new ServiceException(e);
        }
    }

    @Override
    public AdminRoleDO queryUserRole(AdminRoleQuery query) throws ServiceException {
        try {
            return adminRoleDAO.queryUserRole(query);
        } catch (DAOException e) {
            throw new ServiceException(e);
        }
    }

    /**
     * 权限添加
     */
    private void saveAdminRoleRes(List<String> resList, AdminRoleDO postAdminRole) throws DAOException {
        AdminRoleResDO adminRoleResDO = new AdminRoleResDO();
        adminRoleResDO.setRoleId(postAdminRole.getId());
        for (String resId : resList) {
            adminRoleResDO.setResId(Integer.parseInt(resId));
            adminRoleResDO.setUpdateName(postAdminRole.getUpdateName());
            adminRoleResDO.setCreateName(postAdminRole.getUpdateName());
            adminRoleDAO.saveRoleRes(adminRoleResDO);
        }
    }


    @Override
    public void save(AdminRoleDO adminRoleDO) throws ServiceException {
        try {
            AdminRoleDO postAdminRole;
            if (adminRoleDO.getId() != null) {
                //更新
                adminRoleDAO.update(adminRoleDO);
                //权限,先删除,再添加
                adminRoleDAO.deleteRoleRes(adminRoleDO.getId());
                //设置role
                postAdminRole = adminRoleDO;
            } else {
                //新增
                adminRoleDO.setCreateName(adminRoleDO.getUpdateName());
                adminRoleDAO.save(adminRoleDO);
                //设置role
                postAdminRole = adminRoleDO;
            }
            //权限添加
            List<String> resList = adminRoleDO.getResList();
            if (resList != null && resList.size() > 0) {
                saveAdminRoleRes(resList, postAdminRole);
            }
        } catch (DAOException e) {
            throw new ServiceException(e);
        }

    }


    @Override
    public void updateStatus(AdminRoleDO adminRoleDO) throws ServiceException {
        try {
            adminRoleDAO.update(adminRoleDO);
            //更新用户与角色表；禁止或启用当前角色，用户的角色信息会随之更新
            AdminUserRoleDO adminUserRoleDO = new AdminUserRoleDO();
            adminUserRoleDO.setRoleId(adminRoleDO.getId());
            adminUserRoleDO.setStatus(adminRoleDO.getStatus());
            adminUserRoleDO.setUpdateName(adminRoleDO.getUpdateName());
            adminUserDAO.updateUserRoleStatus(adminUserRoleDO);
        } catch (DAOException e) {
            throw new ServiceException(e);
        }
    }


    @Override
    public void deleteBatch(List<Integer> idsList) throws ServiceException {
        try {
            adminRoleDAO.deleteBatch(idsList);
            //删除关联表
            adminRoleDAO.deleteBatchRoleRes(idsList);
        } catch (DAOException e) {
            throw new ServiceException(e);
        }
    }

    @Override
    public void delete(Integer id) throws ServiceException {
        try {
            adminRoleDAO.delete(id);
            //删除关联表
            adminRoleDAO.deleteRoleRes(id);
        } catch (DAOException e) {
            throw new ServiceException(e);
        }
    }
}
